<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            class="input-with-select"
            v-model="queryInfo.query"
            clearable
            @clear="getGoodsList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList()"
            >
            </el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <!-- 添加用户按钮 -->
          <el-button
            type="primary"
            class="butt_tian"
            @click="addDialogVisible = true"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <el-table :data="goodsList" stripe border>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="110px"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="80px"> </el-table-column>
        <el-table-column type="index" label="创建时间" width="100px"> </el-table-column>
        <el-table-column  label="操作" width="160px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit"  size="small"></el-button>
            <el-button
             size="small"
              type="danger"
              icon="el-icon-delete"
              @click="removeRolesById(scope.row.id)"
              ></el-button
            >
            </template
          >
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[1, 2, 5, 10]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    </el-card>
    
  </div>
</template>

<script>
import {
  getGoods,
  removeGoods,
} from "@/api/goods";
export default {
  data() {
    return {
      total: 0,
      goodsList: [],
      addDialogVisible: false,
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
   
    // 获取数据
    async getGoodsList() {
      const res = await getGoods(this.queryInfo);
      // console.log(res);
      if (res.meta.status !== 200) {
        return this.$message.error("获取角色列表失败");
      }
      this.goodsList = res.data.goods;
      // console.log(res);
      this.total = res.data.total;
      // console.log(this.rolesList);
    },
    handleSizeChange(newSize) {
      // console.log("newSize",newSize);
      this.queryInfo.pagesize = newSize;
      this.getGoodsList();
    },
    handleCurrentChange(newPage) {
      // console.log("newPage",newPage);
      this.queryInfo.pagenum = newPage;
      this.getGoodsList();
    },
    // 删除
    removeRolesById(id) {
      this.$confirm("此操作将永久删除该角色, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        // 确认后的操作
      })
        .then(async () => {
          const res = await removeGoods(id);
          if (res.meta.status !== 200) {
            return this.$message.error("删除用户失败！");
          }
          this.$message.success("删除用户成功！");
          this.getGoodsList();
          // 失败后的操作
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style>
.butt_tian {
  margin-bottom: 15px;
}
</style>